package org.zju.demo.shoppingdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.zju.demo.shoppingdemo.R;
import org.zju.demo.shoppingdemo.model.response.BaseResponse;

/**
 * Created by ZJDX on 2015/9/11.
 */
public class UserCenterActivity extends CommonActivity {
    // defined REQUEST_CODE
    private static final int REQUEST_LOGIN = 0;
    private static final int REQUEST_PAYMENT = 1;
    private static final int REQUEST_SHIP = 2;
    private static final int REQUEST_RECEIPT = 3;
    private static final int REQUEST_HISTORY = 4;
    private static final int REQUEST_COLLECT = 5;
    private static final int REQUEST_ADDRESS = 6;
    private static final int REQUEST_HELP = 7;
    // define component
    private TextView loginView =  null;
    private FrameLayout headPaymentFrame = null;
    private FrameLayout headShipFrame = null;
    private FrameLayout headReceiptFrame = null;
    private FrameLayout headHistoryFrame = null;
    private LinearLayout headCollect = null;
    private LinearLayout addressManagent =null;
    private LinearLayout help = null;

    private UCListener ucListener = new UCListener();
    // activity state data
    private boolean isLogin = false;
    private String username = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    // 不显示标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.usercenter);

        super.textViewTitle = (TextView) findViewById(R.id.title);
//        super.textViewTitle.setText("个人中心");

        loginView = (TextView) findViewById(R.id.click_to_login);
        headPaymentFrame = (FrameLayout) findViewById(R.id.profile_head_payment);
        headShipFrame = (FrameLayout) findViewById(R.id.profile_head_ship);
        headReceiptFrame = (FrameLayout) findViewById(R.id.profile_head_receipt);
        headHistoryFrame = (FrameLayout) findViewById(R.id.profile_head_history);
        headCollect = (LinearLayout) findViewById(R.id.profile_head_collect);
        addressManagent = (LinearLayout) findViewById(R.id.profile_head_address_manage);
        help = (LinearLayout) findViewById(R.id.profile_help);

        // register listener
        initBottomMenu();
        loginView.setOnClickListener(ucListener);
        headPaymentFrame.setOnClickListener(ucListener);
        headShipFrame.setOnClickListener(ucListener);
        headReceiptFrame.setOnClickListener(ucListener);
        headHistoryFrame.setOnClickListener(ucListener);
        headCollect.setOnClickListener(ucListener);
        addressManagent.setOnClickListener(ucListener);
        help.setOnClickListener(ucListener);
    }

    class UCListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.click_to_login:
                    // start login activity and return user information
                    if (!isLogin) {
                        Intent intent = new Intent();
                        intent.setClass(UserCenterActivity.this, LoginActivity.class);
                        startActivityForResult(intent, REQUEST_LOGIN);
                    } else {
                        // jump to other activity
                    }
                    break;
                case R.id.profile_head_payment:
                    break;
                case R.id.profile_head_ship:
                    break;
                case R.id.profile_head_receipt:
                    break;
                case R.id.profile_head_history:
                    break;
                case R.id.profile_head_collect:
                    break;
                case R.id.profile_head_address_manage:
                    // start usercenter_addresslist activity
                    if(isLogin) {
                        Intent intent = new Intent();
                        intent.setClass(UserCenterActivity.this, AddressManagementActivity.class);
                        intent.putExtra("username", username);
                        startActivity(intent);
                    } else {
                        Toast.makeText(UserCenterActivity.this, "请登录", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.profile_help:
                    break;
                default:
            }
        }
    }

    private void doAfterLogin(int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            username = data.getStringExtra("username");
            loginView.setText(username);
            isLogin = true;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case REQUEST_LOGIN:
                doAfterLogin(resultCode, data);
                break;
            case REQUEST_PAYMENT:
                break;
            case REQUEST_COLLECT:
                break;
            case REQUEST_ADDRESS:
                break;
            case REQUEST_HISTORY:
                break;
            case REQUEST_RECEIPT:
                break;
            case REQUEST_SHIP:
                break;
            case REQUEST_HELP:
                break;
            default:
        }
    }

    @Override
    public void initBottomMenu() {
        imageViewIndex = (ImageView) findViewById(R.id.menu_home_img);
        imageViewIndex.setOnTouchListener(indexListener);
        imageViewIndex.setImageResource(R.drawable.menu_home_released);

        imageViewShopping = (ImageView) findViewById(R.id.menu_shopping_cart_img);
        imageViewShopping.setOnTouchListener(shoppingListener);
        imageViewShopping.setImageResource(R.drawable.menu_shopping_cart_released);

        imageViewMyInfo = (ImageView) findViewById(R.id.menu_personal_center_image);
//        imageViewMyInfo.setOnTouchListener(myInfoListener);
        imageViewMyInfo.setImageResource(R.drawable.menu_my_info_pressed);
    }

    @Override
    public void onResponseMessage(BaseResponse response) {
        super.onResponseMessage(response);
    }
}
